//
// Created by 13268 on 2025/11/7.
//
#include <bits/stdc++.h>
using namespace std;
#define MAX 1000
struct Person//联系人信息包括（姓名、性别、年龄、联系电话、家庭住址）
{
    string name;
    int gender;
    int age;
    long long tel;
    string address;
};
struct memo_manager
{
    Person personArray[MAX];
    int personArraySize=0;
};
void clear_screen();
void end_operation();
void judge_empty();
void show_menu();
void addPerson(memo_manager *m);
void showperson(const memo_manager *m);
void deletePerson(memo_manager *m);
void search_person(const memo_manager *m);
void change_person(memo_manager *m);
void quit();
int main()
{
    int selection=0;
    memo_manager memo_manager={};
    memo_manager.personArraySize=0;
    while (true)
    {
        show_menu();
        cin>>selection;
        switch (selection)
        {
            case 1: //添加联系人
                addPerson(&memo_manager);
                break;
            case 2://2.显示联系人
                showperson(&memo_manager);
                break;
            case 3://3.删除联系人
                deletePerson(&memo_manager);
                break;
            case 4://4.查找联系人
                search_person(&memo_manager);
                break;
            case 5://5.修改联系人
                change_person(&memo_manager);
                break;
            case 6://6.清空联系人
                memo_manager={};
                memo_manager.personArraySize=0;
                end_operation();
                break;
            case 0://0.退出通讯录
                quit();
                return 0;
            default:
                cout<<"Invalid operation."<<endl;
                end_operation();
        }
    }
}
void clear_screen()
{
    for (int i=0;i<50;i++)
    {
        cout<<endl;
    }
}
void end_operation()
{
    cout << "Press any key to continue..." << endl;
    cin.ignore(numeric_limits<streamsize>::max(),'\n');
    cin.get();
    clear_screen();
}

void show_menu()
{
    cout<<R"(
************************
*****  1.添加联系人  *****
*****  2.显示联系人  *****
*****  3.删除联系人  *****
*****  4.查找联系人  *****
*****  5.修改联系人  *****
*****  6.清空联系人  *****
*****  0.退出通讯录  *****
************************)"<<endl;
}
void addPerson(memo_manager *m)
{
    if (m->personArraySize==MAX)
    {
        cout<<"memo is full."<<endl;
    }else
    {
        string name;
        cout<<"Please input the name of the person to add:"<<endl;
        cin>>name;
        m->personArray[m->personArraySize].name=name;
        int gender;
        cout<<"Please input the gender of the person:"<<endl;
        cout<<"1---male"<<endl;
        cout<<"2---female"<<endl;
        cin>>gender;
        while(true)
        {
            if(gender==1||gender==2)
            {
                m->personArray[m->personArraySize].gender=gender;
                break;
            }else
            {
                cout<<"Wrong gender!"<<endl;
                cin>>gender;
            }
        }
        int age;
        cout<<"Please input the age:"<<endl;
        cin>>age;
        m->personArray[m->personArraySize].age=age;
        long long tel;
        cout<<"Please input the tel:"<<endl;
        cin>>tel;
        m->personArray[m->personArraySize].tel=tel;
        string address;
        cout<<"Please input the address of the person:"<<endl;
        cin>>address;
        m->personArray[m->personArraySize].address=address;
        m->personArraySize++;
    }
    end_operation();
}
void showperson(const memo_manager *m)
{
    if (m->personArraySize==0)
    {
        cout<<"memo is empty."<<endl;
    }else
    {
        for(int i=0; i<m->personArraySize; i++)
        {
            cout<<"记录 "<<i+1<<":"<<"\n";
            cout<<"姓名："<<m->personArray[i].name<<"\t";
            cout<<"性别："<<(m->personArray[i].gender==1?"男":"女")<<"\t";
            cout<<"年龄："<<m->personArray[i].age<<"\t";
            cout<<"电话："<<m->personArray[i].tel<<"\t";
            cout<<"住址："<<m->personArray[i].address<<endl;
        }

    }
    end_operation();
}
void deletePerson(memo_manager *m)
{
    if (m->personArraySize==0)
    {
        cout<<"memo is empty."<<endl;
        end_operation();
    }else
    {
        string target;
        cout<<"Please input the person to delete:"<<endl;
        cin>>target;
        bool found=false;
        int i;
        for (i=0;i<m->personArraySize;i++)
        {
            if (m->personArray[i].name==target)
            {
                cout<<"Target found at index "<<i+1<<endl;
                found=true;
                break;
            }
        }
        if (!found)
        {
            cout<<"Didn't find the target..."<<endl;
        }
        if (found)
        {
            for (;i<m->personArraySize-1;i++)
            {
                m->personArray[i]=m->personArray[i+1];
            }
            m->personArraySize--;
        }
    }
    end_operation();
}
void search_person(const memo_manager *m)
{
    if (m->personArraySize==0)
    {
        cout<<"memo is empty."<<endl;
        end_operation();
    }else
    {
        string target;
        cout<<"Please input the person to search:"<<endl;
        cin>>target;
        bool found=false;
        int i;
        for (i=0;i<m->personArraySize;i++)
        {
            if (m->personArray[i].name==target)
            {
                cout<<"Target found at index "<<i+1<<endl;
                found=true;
                break;
            }
        }
        if (!found)
        {
            cout<<"Didn't find the target..."<<endl;
        }
        if (found)
        {
            cout<<"姓名："<<m->personArray[i].name<<"\t";
            cout<<"性别："<<(m->personArray[i].gender==1?"男":"女")<<"\t";
            cout<<"年龄："<<m->personArray[i].age<<"\t";
            cout<<"电话："<<m->personArray[i].tel<<"\t";
            cout<<"住址："<<m->personArray[i].address<<endl;
        }
    }
    end_operation();
}
void change_person(memo_manager *m)
{
    if (m->personArraySize==0)
    {
        cout<<"memo is empty."<<endl;
        end_operation();
    }else
    {
        string target;
        cout<<"Please input the person to change:"<<endl;
        cin>>target;
        bool found=false;
        int i;
        for (i=0;i<m->personArraySize;i++)
        {
            if (m->personArray[i].name==target)
            {
                cout<<"Target found at index "<<i+1<<endl;
                found=true;
                break;
            }
        }
        if (!found)
        {
            cout<<"Didn't find the target..."<<endl;
        }
        if (found)
        {
            string name;
            cout<<"Please input the name of the person to change:"<<endl;
            cin>>name;
            m->personArray[i].name=name;
            int gender;
            cout<<"Please input the gender of the person:"<<endl;
            cout<<"1---male"<<endl;
            cout<<"2---female"<<endl;
            cin>>gender;
            while(true)
            {
                if(gender==1||gender==2)
                {
                    m->personArray[i].gender=gender;
                    break;
                }else
                {
                    cout<<"Wrong gender!"<<endl;
                    cin>>gender;
                }
            }
            int age;
            cout<<"Please input the age:"<<endl;
            cin>>age;
            m->personArray[i].age=age;
            long long tel;
            cout<<"Please input the tel:"<<endl;
            cin>>tel;
            m->personArray[i].tel=tel;
            string address;
            cout<<"Please input the address of the person:"<<endl;
            cin>>address;
            m->personArray[i].address=address;
        }
    }
    end_operation();
}
void quit()
{
    cout<<"Bye!"<<endl;
}

